<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<script>
    /*
        1. await关键字
            - await的行为与yield是一样的,解包对象的值
     */
    // 1.1 不使用await的时候
    let p = new Promise((resolve => setTimeout(resolve, 1000, 3)))
    p.then(console.log)  // 3

    // 1.2 使用async/await
    async function foo() {
        let p = new Promise((resolve => setTimeout(resolve, 1000, 4)))
        console.log(await p)
    }

    foo();

    // 1.3 更多的例子
    async function a1() {
        console.log(await Promise.resolve('a1'))
    }
    a1(); // a1
    async function a2() {
        return await Promise.resolve('a2')
    }
    a2().then(console.log) // a2
    
    async function a3() {
      let value =  await new Promise(resolve => setTimeout(resolve,1000,'a3'))
        console.log(value)
    }
    a3(); // a3
    
    /*
        2. 错误的情况
     */
    
    // 2.1 await错误
    async function err() {
        console.log('e1')
        await (() => {throw 'e2'})()
    }
    err().catch(console.log) ;  // e2
    console.log('e3')
    
    // 2.2 await reject
    async function err2() {
        console.log('e4')
        await Promise.reject('e5')
        console.log('e6')   // 这个不会执行
    }
    err2().catch(console.log);
    console.log('e7')
    // e4 e7 e5
    
</script>
</body>
</html>